﻿
Partial Class WebForm_ManageStaffRole_SearchStaffRole
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLDivison()
            Me.BindDDLParent()
        End If
    End Sub

    Protected Sub btnSelect_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        Me.BindGV()
    End Sub

    Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand
        If e.CommandName = "Adding" Or e.CommandName = "Deleting" Then
            Dim row As GridViewRow = TryCast(TryCast(e.CommandSource, Control).NamingContainer, GridViewRow)
            Dim key As DataKey = Me.gv.DataKeys(row.RowIndex)

            Me.RoleID = key("RoleID").ToString()

            If e.CommandName = "Adding" Then
                Me.SearchEmployControl1.HasRoleID = Me.RoleID
                Me.SearchEmployControl1.Show()
            ElseIf e.CommandName = "Deleting" Then
                Me.EmployeeID = e.CommandArgument.ToString()
                Me.Deleted()
                Me.BindGV()
            End If
        End If

    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If CType(e.Row.FindControl("lblUserID"), Label).Text = "" Then
                CType(e.Row.FindControl("lknAdd"), LinkButton).Visible = True
                CType(e.Row.FindControl("lknDelete"), LinkButton).Visible = False
                e.Row.Font.Bold = True
            Else
                CType(e.Row.FindControl("lknAdd"), LinkButton).Visible = False
                CType(e.Row.FindControl("lknDelete"), LinkButton).Visible = True
            End If
        End If
    End Sub

    Protected Sub SearchEmployControl1_Saving(ByVal source As Object, ByVal e As System.EventArgs) Handles SearchEmployControl1.Saving
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        If Me.SearchEmployControl1.SelectedUserList.Count > 0 Then
            Using scope As New Transactions.TransactionScope

                For Each userID As String In Me.SearchEmployControl1.SelectedUserList
                    sql.AppendFormat(" insert RoleUserMapping (RoleID, UserID) select ")
                    sql.AppendFormat(" '{0}' ", Me.RoleID)
                    sql.AppendFormat(",'{0}'; ", userID)
                Next

                dat.AccData(sql)
                scope.Complete()

            End Using
        End If

        Me.BindGV()
    End Sub

    'Protected Sub ddlParent_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlParent.DataBound
    '    Me.ddlParent.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    'End Sub

    Protected Sub ddlDivision_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlDivision.SelectedIndexChanged
        Me.BindDDLParent()
    End Sub
End Class

Partial Class WebForm_ManageStaffRole_SearchStaffRole

    Private Sub Deleted()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Delete From RoleUserMapping ")
        sql.AppendFormat(" Where RoleID=@RoleID ")
        sql.AppendFormat(" And UserID=@UserID ")

        sql.AddWithValue("RoleID", Me.RoleID)
        sql.AddWithValue("UserID", Me.EmployeeID)

        dat.AccData(sql)

    End Sub

#Region "資料繫結"
    Private Sub BindDDLDivison()

        Me.ddlDivision.DataTextField = "Text"
        Me.ddlDivision.DataValueField = "Value"

        Me.ddlDivision.DataSource = MyObj.GetCodeMappingData("Product", "Division")
        Me.ddlDivision.DataBind()
        Me.ddlDivision.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
    Private Sub BindDDLParent()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select Division,RoleName from Role ")

        If Me.ddlDivision.SelectedValue <> "" Then
            sql.AppendFormat("Where Division = '{0}' ", ddlDivision.SelectedValue)
        Else
            sql.AppendFormat("Where Division is null ")
        End If


        Me.ddlParent.DataTextField = "RoleName"
        Me.ddlParent.DataValueField = "RoleName"

        Me.ddlParent.DataSource = dat.GetData(sql)
        Me.ddlParent.DataBind()
        Me.ddlParent.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())


    End Sub
    Private Sub BindGV()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0

        sql.AppendFormat(" Select * From ( ")
        sql.AppendFormat(" SELECT ")
        sql.AppendFormat(" parent.RoleName As ParentName, ")
        sql.AppendFormat(" parent.RoleID as ParentID,r.RoleName,r.RoleID,r.Division,'' AS EmployeeID,'' AS EmployeeName ")
        sql.AppendFormat(" FROM [Role] r Inner Join [Role] parent ON r.Parent = parent.RoleID ")
        sql.AppendFormat(" Union ")
        sql.AppendFormat(" Select parent.RoleName As ParentName, ")
        sql.AppendFormat(" parent.RoleID as ParentID,r.RoleName,r.RoleID,r.Division, e.EmployeeID,e.EmployeeName ")
        sql.AppendFormat(" From [Role] r Inner Join [Role] parent ON r.Parent = parent.RoleID ")
        sql.AppendFormat(" Inner Join RoleUserMapping rm ON r.RoleID = rm.RoleID ")
        sql.AppendFormat(" Inner Join Employee e ON e.EmployeeID = rm.UserID ")
        sql.AppendFormat(" ) T ")
        sql.AppendFormat(" Where 1=1 ")


        If Me.ddlDivision.SelectedValue <> "" Then
            sql.AppendFormat(" And Division = @Division ")
            sql.AddWithValue("Division", Me.ddlDivision.SelectedValue)
        End If

        If Me.ddlParent.SelectedValue <> "" Then
            sql.AppendFormat(" And RoleName=@RoleName ")
            sql.AddWithValue("RoleName", Me.ddlParent.SelectedValue)
        End If

        If Me.txtRoleName.Text <> "" Then
            sql.AppendFormat(" And EmployeeID like '%{0}%' ", Me.txtRoleName.Text)
        End If

        sql.AppendFormat(" Order By ParentID,RoleID ")


        Me.gv.DataSource = dat.GetData(sql)
        Me.gv.DataBind()


    End Sub


#End Region

#Region "Event"

#End Region

#Region "Property"
    Public Property EmployeeID() As String
        Get
            Return ViewState("EmployeeID")
        End Get
        Set(ByVal value As String)
            ViewState("EmployeeID") = value
        End Set
    End Property

    Public Property RoleID() As String
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As String)
            ViewState("RoleID") = value
        End Set
    End Property
#End Region
End Class
